package com.tengju.bff.interfaces.manage.common;

import com.tengju.bff.interfaces.manage.common.facade.CommonManageFacade;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.KVVO;
import com.tengju.bff.interfaces.shared.servlet.ManageEntrance;
import com.tengju.user.application.shared.ApplicationException;
import com.tengju.user.domain.shared.DomainException;
import com.tengju.user.domain.shared.JsonUtil;
import com.tengju.user.domain.shared.ResultCodeEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

import static com.tengju.user.domain.shared.ResultCodeEnum.SYS_EXCEPTION;

@RestController
@Api(tags = "通用操作接口")
@RequestMapping(value = "manage/common")
@Slf4j
@ManageEntrance
public class CommonManageController {

    @Resource
    private CommonManageFacade commonManageFacade;

    @Value("${common.tip}")
    private String commonTip;

    @ApiOperation(value = "获取文章类型列表", notes = "获取文章类型列表")
    @GetMapping(value = "getArticleTypes", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getArticleTypes() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getArticleTypes());
        } catch (DomainException | ApplicationException e) {
            log.error("获取文章类型列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取文章类型列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取资源投放位置列表", notes = "获取资源投放位置列表")
    @GetMapping(value = "getResourcePositions", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getResourcePositions() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getResourcePositions());
        } catch (DomainException | ApplicationException e) {
            log.error("获取资源投放位置列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取资源投放位置列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取资源定向列表", notes = "获取资源定向列表")
    @GetMapping(value = "getResourceOrientations", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getResourceOrientations() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getResourceOrientations());
        } catch (DomainException | ApplicationException e) {
            log.error("获取资源定向列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取资源定向列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户等级列表", notes = "获取用户等级列表")
    @GetMapping(value = "getStarLevels", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getStarLevels() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getStarLevels());
        } catch (DomainException | ApplicationException e) {
            log.error("获取用户等级列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取用户等级列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取用户等级列表(ShortName版)", notes = "获取用户等级列表(ShortName版)")
    @GetMapping(value = "getStarLevelsShortName", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getStarLevelsShortName() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getStarLevelsShortName());
        } catch (DomainException | ApplicationException e) {
            log.error("获取用户等级列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取用户等级列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取星级签约状态列表", notes = "获取星级签约状态列表")
    @GetMapping(value = "getStarSignStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getStarSignStatus() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getStarSignStatus());
        } catch (DomainException | ApplicationException e) {
            log.error("获取星级签约状态列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取星级签约状态列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取CRM星级签约状态列表", notes = "获取CRM星级签约状态列表")
    @GetMapping(value = "getCrmStarSignStatus", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getCrmStarSignStatus() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getCrmStarSignStatus());
        } catch (DomainException | ApplicationException e) {
            log.error("获取星级签约状态列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取星级签约状态列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取时间类型列表", notes = "获取时间类型列表")
    @GetMapping(value = "getDateTypeList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getDateTypeList() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getDateTypeList());
        } catch (DomainException | ApplicationException e) {
            log.error("获取时间类型列表", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取时间类型列表", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取季度列表", notes = "获取季度列表")
    @GetMapping(value = "getQuarterList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<String>> getQuarterList() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getQuarterList());
        } catch (DomainException | ApplicationException e) {
            log.error("获取季度列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取季度列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取月度列表", notes = "获取月度列表")
    @GetMapping(value = "getMonthList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> getMonthList() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getMonthList());
        } catch (DomainException | ApplicationException e) {
            log.error("获取月度列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取月度列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取市场补贴月度列表", notes = "获取市场补贴月度列表")
    @GetMapping(value = "getBonusMonthList", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO>> getBonusMonthList() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getBonusMonthList());
        } catch (DomainException | ApplicationException e) {
            log.error("获取月度列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取月度列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "获取经销商用户等级列表", notes = "获取经销商用户等级列表")
    @GetMapping(value = "getUserLevels", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<List<KVVO<String, Integer>>> getUserLevels() {
        try {
            return ApiResponse.newSuccess(commonManageFacade.getUserLevels());
        } catch (DomainException | ApplicationException e) {
            log.error("获取经销商用户等级列表失败", e);
            return ApiResponse.newFailure(ResultCodeEnum.BIZ_ERROR.getCode(), e.getMessage());
        } catch (Exception e) {
            log.error("获取经销商用户等级列表异常", e);
            return ApiResponse.newFailure(SYS_EXCEPTION.getCode(), e.getMessage());
        }
    }

    @ApiOperation(value = "备注说明", notes = "备注说明")
    @GetMapping(value = "desc", produces = MediaType.APPLICATION_JSON_VALUE)
    public ApiResponse<String> tip(@Valid @NotNull @RequestParam(value = "name") String name) {
        Map<String, String> tipMap = JsonUtil.toMap(commonTip);
        if (CollectionUtils.isEmpty(tipMap)) {
            return ApiResponse.newSuccess("");
        }
        return ApiResponse.newSuccess(tipMap.getOrDefault(name,""));
    }
}
